package fashionshop.bll;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;

import fashionshop.dal.CategoryDAL;
import fashionshop.dal.ProductDAL;

public class ProductBLL {
	
	private int id;
	private int categoryId;
	private String name;
	private String code;
	private int quantity;
	private String image;
	private String color;
	private BigDecimal price;
	private String description;
	private String createDate;
	private int active;
	private BigDecimal salePrice;
	
	public ProductBLL() {
//		this.name = "";
//		this.code = "";
//		this.quantity = 0;
//		this.image = "";
//		this.color = "";
//		this.price = BigDecimal.ZERO;
//		this.description = "";
//		this.createDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
//		this.salePrice = BigDecimal.ZERO;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getCategoryId() {
		return categoryId;
	}
	public void setCategoryId(int categoryId) {
		this.categoryId = categoryId;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public int getQuantity() {
		return quantity;
	}
	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}
	public String getImage() {
		return image;
	}
	public void setImage(String image) {
		this.image = image;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public BigDecimal getPrice() {
		return price;
	}
	public void setPrice(BigDecimal price) {
		this.price = price;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getCreateDate() {
		return createDate;
	}
	public void setCreateDate(String createDate) {
		this.createDate = createDate;
	}
	public int getActive() {
		return active;
	}
	public void setActive(int active) {
		this.active = active;
	}
	public BigDecimal getSalePrice() {
		return salePrice;
	}
	public void setSalePrice(BigDecimal salePrice) {
		this.salePrice = salePrice;
	}
	
	public Vector loadAllProduct() throws Exception {
		
		Vector productTableRecords = new Vector();
		ResultSet result = ProductDAL.LoadAll();
		while (result.next()){
			Vector record = new Vector();
            record.add(result.getInt("ID"));
            record.add(result.getString("CategoryName"));
            record.add(result.getString("Name"));
            record.add(result.getString("Code"));
            record.add(result.getInt("Quantity"));
            record.add(result.getString("Image"));
            record.add(result.getString("Color"));
            record.add(result.getString("Price"));
            record.add(result.getString("Description"));
            record.add(result.getString("CreateDate"));
            record.add(result.getString("SalePrice"));
			
            productTableRecords.add(record);
		}
		return productTableRecords;
	}
	
	public Vector loadSelectedProduct(int id) throws Exception {
		
		Vector productTableRecords = new Vector();
		ResultSet result = ProductDAL.LoadByID(id);
		while (result.next()){
			Vector record = new Vector();
            record.add(result.getInt("ID"));
            record.add(result.getString("CategoryName"));
            record.add(result.getString("Name"));
            record.add(result.getString("Code"));
            record.add(result.getInt("Quantity"));
            record.add(result.getString("Image"));
            record.add(result.getString("Color"));
            record.add(result.getString("Price"));
            record.add(result.getString("Description"));
            record.add(result.getString("CreateDate"));
            record.add(result.getString("SalePrice"));
			
            productTableRecords.add(record);
		}
		return productTableRecords;
	}
	
	public boolean SaveAll(Vector productTableRecords) throws Exception {
		
		boolean flag = true;
		for(int i = 0 ; i < productTableRecords.size(); i++){
			Vector vt = (Vector) productTableRecords.get(i);
			if(vt.get(0).toString().isEmpty() || vt.get(0).toString() == null || vt.get(0).toString().equals("")){
				ProductBLL product = new ProductBLL();
				product.setCategoryId(CategoryBLL.getIdFromName(vt.get(1).toString()));
				product.setName(vt.get(2).toString());
				product.setCode(vt.get(3).toString());
				product.setQuantity(Integer.parseInt(vt.get(4).toString()));
				product.setImage(vt.get(5).toString());
				product.setColor(vt.get(6).toString());
				product.setPrice(new BigDecimal(vt.get(7) != null ? vt.get(7).toString() : "0"));
				product.setDescription(vt.get(8).toString());
				product.setCreateDate(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(Calendar.getInstance().getTime()));
				product.setSalePrice(new BigDecimal(vt.get(10) != null ? vt.get(10).toString() : "0"));
				if(ProductDAL.Insert(product) == 0){
					flag = false;
					break;
				}
			}
			else{
				ProductBLL product = new ProductBLL();
				product.setId(Integer.parseInt(vt.get(0).toString()));
				product.setCategoryId(CategoryBLL.getIdFromName(vt.get(1).toString()));
				product.setName(vt.get(2).toString());
				product.setCode(vt.get(3).toString());
				product.setQuantity(Integer.parseInt(vt.get(4).toString()));
				product.setImage(vt.get(5).toString());
				product.setColor(vt.get(6).toString());
				product.setPrice(new BigDecimal(vt.get(7) != null ? vt.get(7).toString() : "0"));
				product.setDescription(vt.get(8).toString());
//				product.setCreateDate(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(Calendar.getInstance().getTime()));
				product.setSalePrice(new BigDecimal(vt.get(10) != null ? vt.get(10).toString() : "0"));
				if(ProductDAL.Update(product) == 0){
					flag = false;
					break;
				}
			}
		}
			
		return flag;
	}
	
	public Vector AdvancedSearch(ProductBLL product, BigDecimal priceForm, BigDecimal priceTo) throws Exception {
		
		Vector productTableRecords = new Vector();
		ResultSet result = ProductDAL.LoadByCriteria(product, priceForm, priceTo);
		while (result.next()){
			Vector record = new Vector();
            record.add(result.getInt("ID"));
            record.add(result.getString("CategoryName"));
            record.add(result.getString("Name"));
            record.add(result.getString("Code"));
            record.add(result.getInt("Quantity"));
            record.add(result.getString("Image"));
            record.add(result.getString("Color"));
            record.add(result.getString("Price"));
            record.add(result.getString("Description"));
            record.add(result.getString("CreateDate"));
            record.add(result.getString("SalePrice"));
			
            productTableRecords.add(record);
		}
		return productTableRecords;
	}
	
	public Vector BasicSearch(String keyword) throws Exception {
		
		Vector productTableRecords = new Vector();
		ResultSet result = ProductDAL.LoadByKeywork(keyword);
		while (result.next()){
			Vector record = new Vector();
            record.add(result.getInt("ID"));
            record.add(result.getString("CategoryName"));
            record.add(result.getString("Name"));
            record.add(result.getString("Code"));
            record.add(result.getInt("Quantity"));
            record.add(result.getString("Image"));
            record.add(result.getString("Color"));
            record.add(result.getString("Price"));
            record.add(result.getString("Description"));
            record.add(result.getString("CreateDate"));
            record.add(result.getString("SalePrice"));
			
            productTableRecords.add(record);
		}
		return productTableRecords;
	}
}
